// pages/user/user.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPwd: true,
    img: "unshow"
  },
  getInfo: function(e) {
    console.log(e)
    if (e.detail.value.userAccount == "") {
      setTimeout(function() {
        wx.showModal({
          title: '提示',
          content: '请输入账号',
          confirmText: "好的",
          confirmColor: "#4fa0e4",
          showCancel: false
        })
      }, 0)
    } else if (e.detail.value.userAccount.length < 6) {
      wx.showModal({
        title: '提示',
        content: '账号长度不能小于6位',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    }
    // else if (e.detail.value.userAccount.length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(e.detail.value.userAccount)) {
    //   wx.showModal({
    //     title: '提示',
    //     content: '请输入正确的手机号码',
    //     confirmText: "好的",
    //     confirmColor: "#4fa0e4",
    //     showCancel: false
    //   })
    // } 
    else if (e.detail.value.userName == "") {
      wx.showModal({
        title: '提示',
        content: '请输入昵称',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else if (e.detail.value.userPwd == "") {
      wx.showModal({
        title: '提示',
        content: '请输入密码',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else if (e.detail.value.userPwd_ok == "") {
      wx.showModal({
        title: '提示',
        content: '请输入确认密码',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else if (e.detail.value.userPwd.length < 6) {
      wx.showModal({
        title: '提示',
        content: '密码长度不能小于6位',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else if (e.detail.value.userPwd_ok != e.detail.value.userPwd) {
      wx.showModal({
        title: '提示',
        content: '确认密码要与密码一致',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else if (e.detail.value.userEmail == "") {
      wx.showModal({
        title: '提示',
        content: '请输入邮箱',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else if (!/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(e.detail.value.userEmail)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的邮箱地址',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else {
      wx.request({
        url: 'https://www.maintenance.ink/student/user/addUserPost',
        header: {
          'content-type': 'application/json;charset=utf-8'
        },
        method: "POST",
        data: {
          userAccount: e.detail.value.userAccount,
          userPwd: e.detail.value.userPwd,
          userEmail: e.detail.value.userEmail,
          userName: e.detail.value.userName
        },
        success: res => {
          console.log("成功", res)
          if (res.data.success) {
            setTimeout(function() {
              setTimeout(function() {
                wx.switchTab({
                  url: '/pages/user/user',
                })
              }, 1000)
              wx.showToast({
                title: '注册成功'
              })
            }, 500)
          } else {
            wx.showToast({
              title: res.data.message,
              icon: "none"
            })
          }
        }
      })
    }
  },
  showPwd() {
    this.setData({
      showPwd: this.data.showPwd ? false : true,
      img: this.data.showPwd ? "show" : "unshow"
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#4fa0e4',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})